(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			initGradeClass();
			initSmPages();
			pageBtns();
		});
	});
	function pageBtns(){
		check("student-name",isNull,"null");
		check("student-fname1",isNull,"null");
		check("student-imei",isNull,"null");
		check("student-cardcode",isNull,"null");
		check("student-cellphone",checkMobilePhone,"checkAddNull");
		check("student-fphone1",checkMobilePhone,"checkAddNull");
		check("student-fcardid1",checkCard,"checkAddNull");
		check("student-studentcode",checkNum,"checkAddNull");
		check("student-grade",isNull,"null");
		check("student-inschooldate",isNull,"null");
		check("student-birdate",isNull,"null");
		
		$("#save-btns").click(function(){
			var isOk = "add";
			if(checkSave(isOk)){
				if(check2(isOk)){
					$("#save-btns").attr("disabled","disabled");
					$("#save-btns").css("background-color","#b5b5b5");
					addStudent(isOk);
				}
			}
		});
		
		$("#ignore-save").click(function(){
			var isOk = "ignore";
			if(checkSave(isOk)){
				if(check2(isOk)){
					$("#ignore-save").attr("disabled","disabled");
					$("#ignore-save").css("background-color","#b5b5b5");
					addStudent(isOk);
				}
			}	
		});
		
		
	}
	
	function addStudent(isOk){
		var tstudent = getFormData(isOk);
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var obj = {};
		for(var key in tstudent){
			if(tstudent[key] != "" && tstudent[key] != undefined){
				obj[key] = tstudent[key];
			}
		}
		obj.schoolcode = schoolcode;
		
		var data = {
				cmd:"addStudent",
				model:"student",
				userKey:userKey,
				data:obj,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				debugger;
				if(res.data.studentcode == "学生编码存在"){
					processError("添加失败，学生编码重复！");
				}else if(res.data.type == "2"){
//					processError("添加失败，成员1联系电话重复！");
					deleteBox("家长编码存在,是否绑定到此家长？",function(){
						var data = {
								cmd:"bindStudent",
								model:"student",
								userKey:userKey,
								data:obj,
								remark:"",						
						};	
						processBegin("正在保存中···");
						AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
							if(res.result == 1 && res.data != undefined && res.data != ""){
								processEnd("添加成功！");
							}else{
								processError("添加失败！");
							}
						});
					});
				}else{
					processEnd("添加成功！");
				}
			}else{
				processError("添加失败！");
			}
			if(isOk == "add"){
				$("#save-btns").removeAttr("disabled","disabled");
				$("#save-btns").css("background-color","#00ABF0");
			}else if(isOk == "ignore"){
				$("#ignore-save").removeAttr("disabled","disabled");
				$("#ignore-save").css("background-color","#00ABF0");
			}
		},null,null);
	}
	
	function getFormData(isOk){
		new setTStudent(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
		var tstudent = getTStudent();
		var ob = {};
		var obj = {};
		for(var key in tstudent){
			if($("#student-"+key)[0]){
				if($("#student-"+key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="birdate"){
						obj[key] = strToDate($("#student-"+key).val());
					}else if(key=="inschooldate"){
						obj[key] = strToDate($("#student-"+key).val());
					}else if(key=="imei"){
						ob[key] = $("#student-"+key).val();
					}else if(key=="cellphone"){
						ob[key] = $("#student-"+key).val();
					}else if(key=="cardcode"){
						ob[key] = $("#student-"+key).val();
					}else{
						obj[key] = encodeURI($("#student-"+key).val());
					}
				}else{
					obj[key] = encodeURI($("#student-"+key+" option:selected").val());
				}
			}
		}
		if(isOk!="ignore"){
			ob.studentcode = obj.studentcode;
			obj.tcard = ob;
		}
		obj.schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		new setTStudent(obj.tcard,obj.name,obj.birdate,obj.sex,obj.oldname,obj.country,obj.birplace,obj.nation,obj.political,obj.hukounature,obj.hukounature,obj.familyadress,obj.familyphone,obj.isonechild,obj.phone,obj.grade,obj.classcode,obj.studentcode,obj.inschooldate,obj.distance,obj.waytype,obj.inschooltype,obj.studytype,obj.fname1,obj.fphone1,obj.fcardtype1,obj.fcardid1,obj.fisguardian1,obj.faddress1,obj.frelation1,obj.fnation1,obj.fhukouplacecode1,obj.fcom1,obj.fjob1,obj.fremark1,obj.fname2,obj.fphone2,obj.fcardtype2,obj.fcardid2,obj.fisguardian2,obj.faddress2,obj.frelation2,obj.fnation2,obj.fhukouplacecode2,obj.fcom2,obj.fjob2,obj.fremark2,obj.imei,obj.cellphone,obj.cardcode,obj.schoolcode);
		return getTStudent();	
	}
	
	function check2(flag){
		if(flag == "ignore"){
			$("#student-imei-ex").text("");
			$("#student-cellphone-ex").text("");
			$("#student-cardcode-ex").text("");
		}
		var flg = false;
		var checkFails = $(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	
	function checkSave(flg){ 
		if(flg == "ignore"){
			if($("#student-name").val() == ""){
				$("#student-name").focus();
				$("#student-name-ex").text("不能为空！");
				return false;
			}else if($("#student-birdate").val() == ""){
				$("#student-birdate").focus();
				$("#student-birdate-ex").text("不能为空！");
				return false;
			}else if($("#student-grade").val() == "班级"){
				$("#student-grade-ex").focus();
				return false;
			}else if($("#student-studentcode").val() == ""){
				$("#student-studentcode-ex").focus();
				$("#student-studentcode-ex").text("不能为空！");
				return false;
			}else if($("#student-inschooldate").val() == ""){
				$("#student-inschooldate-ex").focus();
				$("#student-inschooldate-ex").text("不能为空！");
				return false;
			}else if($("#student-fname1").val() == ""){
				$("#student-fname1-ex").focus();
				$("#student-fname1-ex").text("不能为空！");
				return false;			
			}else if($("#student-fphone1").val() == "" ){
				$("#student-fphone1-ex").focus();
				$("#student-fphone1-ex").text("不能为空！");
				return false;
			}else if($("#student-fcardid1").val() == ""){
				$("#student-fcardid1-ex").focus();
				$("#student-fcardid1-ex").text("不能为空！");
				return false;
			}else{
				return true;
			}
			
		}else if(flg == "add"){
			if($("#student-name").val() == ""){
				$("#student-name").focus();
				$("#student-name-ex").text("不能为空！");
				return false;
			}else if($("#student-birdate").val() == ""){
				$("#student-birdate").focus();
				$("#student-birdate-ex").text("不能为空！");
				return false;
			}else if($("#student-grade").val() == "班级"){
				$("#student-grade-ex").focus();
				return false;
			}else if($("#student-studentcode").val() == ""){
				$("#student-studentcode").focus();
				$("#student-studentcode-ex").text("不能为空！");
				return false;
			}else if($("#student-inschooldate").val() == ""){
				$("#student-inschooldate").focus();
				$("#student-inschooldate-ex").text("不能为空！");
				return false;
			}else if($("#student-fname1").val() == ""){
				$("#student-fname1").focus();
				$("#student-fname1-ex").text("不能为空！");
				return false;			
			}else if($("#student-fphone1").val() == "" ){
				$("#student-fphone1").focus();
				$("#student-fphone1-ex").text("不能为空！");
				return false;
			}else if($("#student-fcardid1").val() == ""){
				$("#student-fcardid1").focus();
				$("#student-fcardid1-ex").text("不能为空！");
				return false;
			}else if($("#student-imei").val() == ""){
				$("#student-imei").focus();
				$("#student-imei-ex").text("不能为空！");
				return false;
			}else if($("#student-cardcode").val() == ""){
				$("#student-cardcode").focus();
				$("#student-cardcode-ex").text("不能为空！");
				return false;
			}else if($("#student-cellphone").val() == "" ){
				$("#student-cellphone").focus();
				$("#student-cellphone-ex").text("不能为空！");
				return false;    			
			}else{
				return true;
			}
		}
	};
	
	function initGradeClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "<option value='' >学段</option>";
			var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
			var classOpt = "<option value='' classes=''>班级</option>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				classCaches = data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#student-type").html(typeOpt);
				
				$("#student-type").unbind("change");
				$("#student-type").change(function(){
					var gradeData = null;
					var type = $("#student-type option:selected").val();
					var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#student-grade").html(gradeOpt);
					
					var classOpt = "<option value='' classes=''>班级</option>";
					$("#student-classcode").html(classOpt);
				});
				
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#student-classcode").html(classOpt);
				});
			}else{
				$("#student-type").html(typeOpt);
				$("#student-grade").html(gradeOpt);
				$("#student-classcode").html(classOpt);
			}
		},null,null);
	}

	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
	
})();